package com.kuang.springcloud.controller;

import com.kuang.springcloud.entities.CommonResult;
import com.kuang.springcloud.entities.Payment;
import com.kuang.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "say1")
public class PaymentController
{
    @Autowired
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient client;

    @GetMapping("say")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "say2")
    /*@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),  //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value ="10"),请求次数
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"),时间窗口期
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60") 失败率达到多少后跳闸
    })
    */
    public Object say()
    {
        /*try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        int i=1/0;
        log.info("hello");
        return "hello";
    }
    public String say2(){
        return "say 的熔断";
    }
    //默认开启全局熔断
    public String say1(){
        return "全局 的熔断";
    }
    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment)
    {
        int result = paymentService.create(payment);
        log.info("****插入结果"+result);
        if (result>0)
        {
            return new CommonResult(200,"插入数据库成功",result);
        }else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }
    @GetMapping("/payment/get/{id}")
    @HystrixCommand(commandProperties = {//设置超时时间5秒
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
            @HystrixProperty(name = "execution.timeout.enabled", value = "false")},fallbackMethod = "fallbackGetPayment")
    public CommonResult getPayment(@PathVariable("id")long id)
    {
        if (id==1L)
        {
            throw new RuntimeException("用户不能为1");
        }
        Payment payment = paymentService.getPaymentById(id);
        log.info("****查询结果"+payment);
        if (payment!=null)
        {
            return new CommonResult(200,"查询8001数据库成功",payment);
        }else {
            return new CommonResult(444,"查询8001数据库失败",null);
        }
    }
    public CommonResult fallbackGetPayment(long id){
        return new CommonResult(444,"已开始服务器熔断，请访问其他服务",null);
    }
    @GetMapping("/payment/{id}")
    public Object getPaymentById(@PathVariable("id") long id)
    {

       return paymentService.getPaymentById(id);
    }

    
    @GetMapping("/payment/discovery")
    @HystrixCommand
    public Object discovery()
    {
        List<String> services = client.getServices();
        int i=1/0;
        for (String service : services) {
            log.info("****element"+service);
        }
        List<ServiceInstance> instances = client.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.client;
    }
}
